package com.thon.service.project.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.PreferentialDao;
import com.thon.dao.project.ProjectCommentDao;
import com.thon.dao.project.ProjectDao;
import com.thon.dao.project.ProjectSupportDao;
import com.thon.dao.user.UserDao;
import com.thon.dao.util.Page;
import com.thon.entity.project.Preferential;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectComment;
import com.thon.entity.project.ProjectCommentView;
import com.thon.entity.user.User;
import com.thon.service.project.ProjectCommentService;

/**
 * @file ProjectCommentServiceImpl.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月19日 上午8:55:04
 * @description: TODO
 */
@Service
@Transactional
public class ProjectCommentServiceImpl implements ProjectCommentService {

	@Autowired
	private ProjectCommentDao projectCommentDao;
	@Autowired
	private ProjectSupportDao projectSupportDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ProjectDao projectDao;
	@Autowired
	private PreferentialDao preferentialDao;
	

	@Override
	public void add(ProjectComment projectComment) {

		projectCommentDao.save(projectComment);
	}

	@Override
	public Page<ProjectCommentView> findComment(String by, int id, int pageNo,
			int pageSize) {

		Page<ProjectComment> projectCommentPage = new Page<ProjectComment>();
		Page<ProjectCommentView> projectCommentViewPage = new Page<ProjectCommentView>();
		projectCommentPage.setPageNo(pageNo);
		projectCommentPage.setPageSize(pageSize);
		
		projectCommentDao.findProjectCommentByProject(projectCommentPage, by,
				id);
		List<ProjectCommentView> projectCommentViews = new ArrayList<ProjectCommentView>();
		for (ProjectComment projectComment : projectCommentPage.getResult()) {
			if(by.equals("comment_to")){
				projectComment.setIsRead(1);
				projectCommentDao.update(projectComment);
			}
			ProjectCommentView projectCommentView = new ProjectCommentView();

			projectCommentView.setId(projectComment.getId());

			User user = userDao.get(projectComment.getUserId());
			if(user!=null){
				projectCommentView.setUserId(user.getId());
				projectCommentView.setUserAttachment(user.getAvatarUrl());
				projectCommentView.setUserName(user.getName());
				//projectCommentView.setUserRole(user.getRoles());
			}
			projectCommentView.setUserContent(projectComment.getContent());
			projectCommentView.setUserCreatedDate(projectComment
					.getCreatedDate());
			projectCommentView.setUserCommentIsDelete(projectComment.getIsDelete());

			if (projectComment.getSuperComment() != null) {
				User friend = userDao.get(projectComment.getSuperComment()
						.getUserId());
				if(friend!=null){
				projectCommentView.setSuperId(friend.getId());
				projectCommentView.setSuperAttachment(friend.getAvatarUrl());
				projectCommentView.setSuperName(friend.getName());
				//projectCommentView.setSuperRole(friend.getRoles());
				}
				projectCommentView.setSuperContent(projectComment
						.getSuperComment().getContent());
				projectCommentView.setSuperCreatedDate(projectComment
						.getSuperComment().getCreatedDate());
				projectCommentView.setSuperIsDelete(projectComment.getSuperComment().getIsDelete());
			}

			Project project = projectComment.getProject();
			if(project==null)
			{
				Preferential preferential =projectComment.getPreferential();
				if(preferential!=null){
				projectCommentView.setPreferentialId(preferential.getId());
				projectCommentView.setPreferentialImg(preferential.getPhotoUrl());
				projectCommentView.setPreferentialName(preferential.getName());
				projectCommentView.setPreferentialStatus(preferential.getStatus());
				projectCommentView.setPreferentialCategory(preferential.getProjectCategory().getCategoryName());
				//projectCommentView.setFundCurrent(preferential.getFundCurrent());
				//projectCommentView.setSupportCount(projectDao.count(						"supportCount", preferential.getId()).intValue());
				}
			}
			else {
				projectCommentView.setProjectId(project.getId());
				projectCommentView.setProjectImg(project.getPhotoUrl());
				projectCommentView.setProjectName(project.getName());
				projectCommentView.setFundCurrent(projectSupportDao.sumSaleCurrent(project.getId().intValue()));
				projectCommentView.setSupportCount(projectSupportDao.countProductAmount(project.getId()).intValue());
			}
			
			
			projectCommentViews.add(projectCommentView);
		}

		projectCommentViewPage.setPageNo(projectCommentPage.getPageNo());
		projectCommentViewPage.setPageSize(projectCommentPage.getPageSize());
		projectCommentViewPage.setResult(projectCommentViews);
		projectCommentViewPage
				.setTotalCount(projectCommentPage.getTotalCount());
		return projectCommentViewPage;
	}

	@Override
	public Long countCommentToUser(Integer isRead,Integer projectId,Integer preferentionId,Integer userId) {
		if(projectId.equals(0))
			return projectCommentDao.countUnreadPreferentionComment(isRead,preferentionId,userId);
		else 
			return projectCommentDao.countUnreadProjectComment(isRead,projectId,userId);
	}

	@Override
	public void updateComment(ProjectComment projectComment) {
		
		projectCommentDao.update(projectComment);
	}

	@Override
	public ProjectComment getComment(Integer commentId) {
		
		return projectCommentDao.get(commentId);
	}

	@Override
	public Page<ProjectCommentView> findMyComments(Page<ProjectCommentView> commentViews,Integer userId,Integer method,
			Integer pageNo, Integer pageSize) {
		Page<ProjectComment> comments = new Page<ProjectComment>();
		Long recordCount = (long) 0;
		comments.setPageNo(pageNo);
		comments.setPageSize(pageSize);
		
		projectCommentDao.findCommentByUser(comments,userId);
		List<ProjectCommentView> lCommentViews=new ArrayList<ProjectCommentView>();
		for(ProjectComment comment:comments.getResult()){
			
			ProjectCommentView commentView=new ProjectCommentView();
			
			commentView.setUserId(userId);
			commentView.setUserCreatedDate(comment.getCreatedDate());
			commentView.setUserContent(comment.getContent());
			User user = userDao.get(userId);
			if(user==null)
				continue;
			commentView.setUserAttachment(user.getAvatarUrl());
			commentView.setUserCommentIsDelete(comment.getIsDelete());
			commentView.setUserName(user.getName());
			//commentView.setUserRole(user.getRoles());
			commentView.setId(comment.getId());
			ProjectComment superComment = comment.getSuperComment();
			if(superComment!=null)
			{
				commentView.setSuperId(superComment.getId());
				commentView.setSuperIsDelete(superComment.getIsDelete());				
				commentView.setSuperContent(superComment.getContent());
				commentView.setSuperCreatedDate(superComment.getCreatedDate());
				User superUser = userDao.get(userId);
				
				if(superUser == null)
					continue;
				commentView.setSuperAttachment(superUser.getAvatarUrl());
				commentView.setSuperName(superUser.getName());
				//commentView.setSuperRole(superUser.getRoles());
			}
			
			if(method.equals(0)){		
				if (comment.getProject() == null) {					
							if(comment.getPreferential()!=null){
								Preferential preferential = preferentialDao.get(comment.getPreferential().getId());
					commentView.setPreferentialId(preferential.getId());
					commentView.setPreferentialImg(preferential.getPhotoUrl());
					commentView.setPreferentialName(preferential.getName());
					commentView.setPreferentialStatus(preferential.getStatus());
					commentView.setPreferentialCategory(preferential.getProjectCategory().getCategoryName());}
				} else {
					Project project = projectDao.get(comment.getProject().getId());          
					commentView.setProjectId(project.getId());
					commentView.setProjectImg(project.getPhotoUrl());
					commentView.setProjectName(project.getName());
					commentView.setFundCurrent(projectSupportDao.sumSaleCurrent(project.getId().intValue()));
					commentView.setSupportCount(projectSupportDao.countProductAmount(project.getId()).intValue());
				}
				recordCount++; 
			lCommentViews.add(commentView);
			}
			else	if(method.equals(1)){			
				if(comment.getProject()!=null)
				{	
					Project project = projectDao.get(comment.getProject().getId());          
				commentView.setProjectId(project.getId());
				commentView.setProjectImg(project.getPhotoUrl());
				commentView.setProjectName(project.getName());	
				commentView.setFundCurrent(projectSupportDao.sumSaleCurrent(project.getId().intValue()));				
				commentView.setSupportCount(projectSupportDao.countProductAmount(project.getId()).intValue());
					recordCount++;
				lCommentViews.add(commentView);
				}
				}
			else if(method.equals(2)){
				if(comment.getPreferential()!=null){
					Preferential preferential = preferentialDao.get(comment.getPreferential().getId());
					commentView.setPreferentialId(preferential.getId());
					commentView.setPreferentialImg(preferential.getPhotoUrl());
					commentView.setPreferentialName(preferential.getName());	
					commentView.setPreferentialStatus(preferential.getStatus());
					commentView.setPreferentialCategory(preferential.getProjectCategory().getCategoryName());
						recordCount++;
						lCommentViews.add(commentView);
					}
					}
		}
		
		commentViews.setAutoCount(comments.getAutoCount());
		commentViews.setPageNo(comments.getPageNo());
		commentViews.setPageSize(comments.getPageSize());
		commentViews.setResult(lCommentViews);
		
		commentViews.setTotalCount(recordCount);
		   
		
		return commentViews;
	}

}
